package com.ruoyi.web.controller.travel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.travel.domain.TravelItinerary;
import com.ruoyi.travel.service.ITravelItineraryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/travel/itinerary")
public class TravelItineraryController extends BaseController {

    @Autowired
    private ITravelItineraryService itineraryService;

    /** 查询行程库列表 */
    @PreAuthorize("@ss.hasPermi('travel:itinerary:list')")
    @GetMapping("/list")
    public TableDataInfo list(TravelItinerary itinerary) {
        startPage();
        List<TravelItinerary> list = itineraryService.selectTravelItineraryList(itinerary);
        return getDataTable(list);
    }

    /** 获取行程库详情（含内容） */
    @PreAuthorize("@ss.hasPermi('travel:itinerary:query')")
    @GetMapping("/{itineraryId}")
    public AjaxResult getInfo(@PathVariable Long itineraryId) {
        return success(itineraryService.selectTravelItineraryWithContent(itineraryId));
    }

    /** 新增行程库 */
    @PreAuthorize("@ss.hasPermi('travel:itinerary:add')")
    @Log(title = "行程库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TravelItinerary itinerary) {
        int rows = itineraryService.insertTravelItinerary(itinerary);
        if (rows > 0) {
            // 新增成功后返回包含ID的行程库对象
            return success(itinerary);
        }
        return error("新增失败");
    }

    /** 修改行程库 */
    @PreAuthorize("@ss.hasPermi('travel:itinerary:edit')")
    @Log(title = "行程库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TravelItinerary itinerary) {
        return toAjax(itineraryService.updateTravelItinerary(itinerary));
    }

    /** 删除行程库 */
    @PreAuthorize("@ss.hasPermi('travel:itinerary:remove')")
    @Log(title = "行程库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itineraryIds}")
    public AjaxResult remove(@PathVariable Long[] itineraryIds) {
        return toAjax(itineraryService.deleteTravelItineraryByItineraryIds(itineraryIds));
    }
}
